﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using FlyHorse.LIMS.WinApp.Utils;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.AuthorityManageForms.RoleForms
{
    public partial class RoleAuthorityForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.ResourceBll resourceBll = new BLL.ResourceBll();
        BLL.RoleBll roleBll = new BLL.RoleBll();
        Role _role;
        public RoleAuthorityForm(Role role)
        {
            InitializeComponent();
            _role = role;
        }

        private void RoleAuthorityForm_Load(object sender, EventArgs e)
        {
            this.treeList1.KeyFieldName = "ResourceID";
            this.treeList1.ParentFieldName = "PARENTID";
            
            treeList1.OptionsView.ShowCheckBoxes = true;         //是否显示CheckBox
            treeList1.OptionsBehavior.AllowIndeterminateCheckState = false;
            CreateNode();
            this.treeList1.ExpandAll();
        }
        private void CreateNode()
        {
            var resourceList = resourceBll.LoadEntities(m => true).ToList();
            List<ResourceData> resourceDataList = new List<ResourceData>();
            foreach (var resource in resourceList)
            {
                ResourceData resourceData = new ResourceData();
                resourceData.ResourceID = resource.ResourceID;
                resourceData.ResourceName = resource.ResourceName;
                resourceData.PARENTID = resource.ParentID;
                resourceDataList.Add(resourceData);
            }
            this.treeList1.DataSource = resourceDataList;
            this.treeList1.RefreshDataSource();
            InitCheckState();
        }


        private void InitCheckState()
        {
            if (_role.Resources!=null)
            {
                foreach (var resource in _role.Resources)
                {
                    TreeListNode node = this.treeList1.FindNodeByFieldValue("ResourceID", resource.ResourceID);
                    if (node != null)
                    {
                        node.CheckState = CheckState.Checked;
                    }
                }
            }
        }


        private void LargeButtonItem_Save_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<Resource> resList = new List<Resource>();
            _role.Resources = RecursionNodes(this.treeList1.Nodes, resList);
            int result = roleBll.EditRoleResource(_role);
            string msg = string.Format(ResourceOverall.MessBoxSHowCount, result);
            XtraMessageBox.Show(msg);
        }


        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="resList"></param>
        /// <returns></returns>
        private List<Resource> RecursionNodes(TreeListNodes nodes, List<Resource> resList)
        {
            foreach (TreeListNode node in nodes)
            {
                if (node.CheckState != CheckState.Unchecked)
                {
                    string id = Convert.ToString(node.GetValue("ResourceID"));
                    Resource res = resourceBll.LoadEntities(m => m.ResourceID == id).First();
                    resList.Add(res);
                }

                if (node.Nodes.Count > 0)
                {
                    RecursionNodes(node.Nodes, resList);
                }
            }
            return resList;
        }


    }
}